package sv.ues.fia.fragmentcontrol;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import sv.ues.fia.R;
import sv.ues.fia.bdcontrol.ControlBDHelper;
import sv.ues.fia.clases.SearchListItem;
import sv.ues.fia.clases.SearchListItemAdapter;
import sv.ues.fia.entity.Defensa;
import sv.ues.fia.entity.DetalleNota;
import sv.ues.fia.entity.Integrante;

public class NotaFragment extends Fragment {

	public int currentPage;
	/** DECLARAR AQUI LOS OBJETOS VIEW **/
	public ListView lvResultNota;
	public List<DetalleNota> listResultNota;
	public List<SearchListItem> listItemsResult;
	public SearchListItemAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/** Getting the arguments to the Bundle object */
		Bundle data = getArguments();

		/** Getting integer data of the key current_page from the bundle */
		currentPage = data.getInt("current_page", 0);
		listItemsResult = new ArrayList<SearchListItem>();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = null;
		/** Inflar el layout correcto para cada pagina **/

		if (currentPage == 1) {

			v = inflater.inflate(R.layout.activity_detallenota_insertar,
					container, false);
			final ControlBDHelper db = new ControlBDHelper(this.getActivity());

			// Listas para los espinner
			List<Integrante> listCarne = null;
			List<Defensa> listDefensa = null;
			
			Button btnSave = (Button) v.findViewById(R.id.btnSaveDetalleNota);
			final Spinner tipoCarne = (Spinner) v.findViewById(R.id.spinnerIdCarneDetalle);
			final Spinner tipoDefensa = (Spinner) v.findViewById(R.id.spinnerIdDefensaDetalle);
			final EditText edtNotaObs = (EditText) v.findViewById(R.id.edtNotaObs);
			final EditText edtNotaDir = (EditText) v.findViewById(R.id.edtNotaDir);
			
			//Llenar los spinners 
			/** CONSULTAR BD PARA LLENAR ESPINNERS **/
			db.abrir();
			listDefensa= db.consultarDefensa("", "","");
			listCarne= db.consultarIntegrante("","");
			db.cerrar();
			
			ArrayAdapter<Integrante> spinnerIntAdapter = new ArrayAdapter<Integrante>(getActivity(),android.R.layout.simple_spinner_dropdown_item, listCarne);
			tipoCarne.setAdapter(spinnerIntAdapter);
			ArrayAdapter<Defensa> spinnerDefensaAdapter = new ArrayAdapter<Defensa>(getActivity(),android.R.layout.simple_spinner_dropdown_item, listDefensa);
			tipoDefensa.setAdapter(spinnerDefensaAdapter);
			
			btnSave.setOnClickListener( new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					if(edtNotaObs.getText().toString().isEmpty() || edtNotaDir.getText().toString().isEmpty()){
						Toast.makeText(getActivity(), getActivity().getString(R.string.emptyForm),
								Toast.LENGTH_LONG).show();
					}else{
					
						Defensa def = (Defensa)tipoDefensa.getSelectedItem();
						Integrante carne = (Integrante) tipoCarne.getSelectedItem();
						
						DetalleNota nota = new DetalleNota();
						nota.setIdTG(def.getIdTG());
						nota.setIdCarne(carne.getIdCarne());
						nota.setIdDefensa(def.getIdDefensa());
						nota.setNotaDirector(Float.parseFloat(edtNotaDir.getText().toString()));
						nota.setNotaObservador(Float.valueOf(edtNotaObs.getText().toString()));
						
						db.abrir();
						Toast.makeText(getActivity(), db.insertarNota(nota),Toast.LENGTH_LONG).show();
						db.cerrar();
					
					}
				}
			});

		} else if (currentPage == 2) {

			v = inflater.inflate(R.layout.activity_detallenota_buscar,
					container, false);
			final ControlBDHelper db = new ControlBDHelper(this.getActivity());
			
			lvResultNota = (ListView) v.findViewById(R.id.ListResultNotas);
			
			final EditText edtIdDef = (EditText) v.findViewById(R.id.edtIdDefensa);
			final EditText edtCarne = (EditText) v.findViewById(R.id.edtCarneDatalleNota);
			
ListView search = (ListView) v.findViewById(R.id.listOption);
			
			/** LISTENER DEL BOTON BUSCAR **/
			search.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					
					/** CONSULTAR LISTADO DE PERFILES SEGUN ID O NOMBRE **/
					db.abrir();					
					listResultNota = db.consultarNota(edtIdDef.getText().toString(), edtCarne.getText().toString() );					
					db.cerrar();
					
					/** VACIAR RESULTADOS ANTES DE AGREGAR LOS NUEVOS **/
					listItemsResult.clear();
					
					/** LLENAR OTRA LISTA SOLO PARA AGREGARLO AL LISTVIEW **/
					for(DetalleNota x: listResultNota){
						
						SearchListItem item = new SearchListItem();
						
						// Se le asigna un TITULO 
						item.setTitle(getActivity().getString(R.string.idDefensaDetalleForm)+" "+ String.valueOf(x.getIdDefensa()));
						
						// Se le asigna un SUBTITULO
						item.setSubTitle(getActivity().getString(R.string.idCarneDetalleForm) +" "+x.getIdCarne());
						listItemsResult.add(item);						
					}
					
					adapter.notifyDataSetChanged();
	     
				}

			});
			
			/** SE CREA EL ADAPTER PERSONALIZADO PARA EL LISTVIEW **/
			adapter = new SearchListItemAdapter(this.getActivity(),listItemsResult);
			lvResultNota.setAdapter(adapter);
			this.getActivity().registerForContextMenu(lvResultNota);

		}
		return v;
	}

}
